package extractor.view.entities;

import entity.model.Entity;
import entity.model.EntityModel;
import entity.model.members.Attribute;
import entity.model.relationships.Relationship;
import fx.FX;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import laravel_generator.LaravelGenerator;
import laravel_generator.ModelClassLoader;

import java.lang.reflect.Field;


/**
 * Created by nuno-rafael-figueiredo on 23-03-2014.
 */
public class EntitiesView extends BorderPane {
    @FXML
    private TableView<Entity> entitiesView;

    @FXML
    private TableColumn<Entity, String> entityCol;

    @FXML
    private TableColumn<Entity, String> baseEntityCol;

    @FXML
    private TableView<Attribute> attributesView;

    @FXML
    private TableColumn<Attribute, String> attributeNameCol;

    @FXML
    private TableView<Relationship> relationshipsView;

    @FXML
    private TableColumn<Relationship, Class> relationshipTypeCol;

    @FXML
    private TableColumn<Relationship, Entity> relationshipPossessionCol;

    @FXML
    private TableColumn<Relationship, Field> relationshipFieldCol;

    @FXML
    private TableColumn<Relationship, String> relationshipOwnerPropertyCol;

    @FXML
    private TableColumn<Relationship, String> relationshipPossessionPropertyCol;

    @FXML
    private Button loadEntityModelButton;

    @FXML
    private Button generateLaravelButton;

    @FXML
    private Button cancelButton;

    public EntitiesView() {
        FX.load(this);
        FX.popup(this);
    }

    @FXML
    private void initialize(){
        entitiesView.getSelectionModel().selectedItemProperty().addListener((value, before, after) -> onSelect(after));

        entityCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        baseEntityCol.setCellValueFactory(new PropertyValueFactory<>("baseEntityName"));

        attributeNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

        relationshipTypeCol.setCellValueFactory(new PropertyValueFactory<>("class"));
        relationshipPossessionCol.setCellValueFactory(new PropertyValueFactory<>("possession"));
        relationshipFieldCol.setCellValueFactory(new PropertyValueFactory<>("field"));
        relationshipOwnerPropertyCol.setCellValueFactory(new PropertyValueFactory<>("ownerPropertyName"));
        relationshipPossessionPropertyCol.setCellValueFactory(new PropertyValueFactory<>("possessionPropertyName"));

        update();
        EntityModel.INSTANCE.ENTITY_MAP.addListener((MapChangeListener<Class<?>, Entity>) change -> update());

        loadEntityModelButton.setOnAction(e -> load());
        generateLaravelButton.setOnAction(e -> LaravelGenerator.execute());
        cancelButton.setOnAction(e -> FX.close(this));
    }

    private void load() {
        EntityModel.INSTANCE.make(ModelClassLoader.ALL_CLASSES);
        loadEntityModelButton.setDisable(true);
        generateLaravelButton.setDisable(false);
    }

    private void update() {
        entitiesView.setItems(FXCollections.observableArrayList(EntityModel.INSTANCE.ENTITY_MAP.values()));
    }

    private void onSelect(Entity entity) {
        if (entity == null){
            attributesView.getItems().clear();
            relationshipsView.getItems().clear();
        }else {
            attributesView.setItems(FXCollections.observableArrayList(entity.getAttributes()));
            relationshipsView.setItems(FXCollections.observableArrayList(entity.getRelationships()));
        }
    }
}
